package com.example.userp.app.myapplication.RcycleView;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.userp.app.myapplication.R;


/**
 * Describe:
 * Created by ysy on 2018/9/18.
 */
public class MineRecycleView extends RecyclerView.ItemDecoration {

    final static String TAG = "tag";

    int color = Color.BLUE;
    int mHeight = 10;

    public MineRecycleView() {
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = mHeight;
        outRect.right = mHeight;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);

        int childCount = parent.getChildCount();
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(mHeight);
        paint.setAntiAlias(true);
        int height = parent.getHeight();
        for (int i = 0; i < childCount; i++) {
            View childAt = parent.getChildAt(i);
            int childAdapterPosition = parent.getChildAdapterPosition(childAt);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childAt.getLayoutParams();
            float top = childAt.getTop();
            Log.e(TAG, "onDraw: " + layoutParams.rightMargin);
            Log.e(TAG, "parent.getLeft() +: " + parent.getLeft() );
            float left =  parent.getPaddingLeft() + layoutParams.leftMargin;
            float right = parent.getWidth() - parent.getPaddingRight() - mHeight - layoutParams.rightMargin;
            float bottom = childAt.getBottom() + parent.getPaddingBottom();
            if (top - this.mHeight > parent.getPaddingTop() && top - this.mHeight < height - parent.getPaddingBottom()) {
                c.drawRect(left, top - this.mHeight, right, top, paint);
            }

//            float dividerTop = childAt.getTop() - height;
//            float dividerLeft = parent.getPaddingLeft();
//            float dividerBottom = childAt.getTop();
//            float dividerRight = parent.getWidth() - parent.getPaddingRight();
//            Log.e(TAG, "childAt.getTop()=="+childAt.getTop() +"   childAt.getBottom()=="+childAt.getBottom()+"   childAt.getLeft()=="+childAt.getLeft()+" childAt.getRight=="+ childAt.getRight());
//            c.drawRect(dividerLeft,dividerTop,dividerRight,dividerBottom,paint);

        }
    }
}
